package com.yfp.client.controller;

import cn.hutool.core.util.BooleanUtil;
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderV3Result;
import com.yfp.client.domain.dto.*;
import com.yfp.client.domain.vo.ClientBalanceVO;
import com.yfp.client.domain.vo.ClientBillVO;
import com.yfp.client.domain.vo.StoredValueActivityVO;
import com.yfp.client.service.ClientBillService;
import com.yfp.client.service.ClientService;
import com.yfp.client.service.StoredValueActivityService;
import com.yfp.client.util.Response;
import com.yfp.common.core.domain.R;
import com.yfp.common.core.web.controller.BaseController;
import com.yfp.client.domain.dto.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/client/stored")
@Tag(name = "钱包")
@Slf4j
public class StoredValueActivityController extends BaseController {

    @Resource
    private ClientService clientService;
    @Resource
    private ClientBillService clientBillService;
    @Resource
    private StoredValueActivityService storedValueActivityService;

    @Operation(summary = "获取账户余额")
    @GetMapping("/balance")
    public Response<ClientBalanceVO> balance() {
        ClientBalanceVO clientBalance = clientService.balance();
        return Response.success(clientBalance);
    }

    @Operation(summary = "获取储值金额列表")
    @GetMapping("/list/activity")
    public Response<StoredValueActivityVO> getOngoingActivity() {
        StoredValueActivityVO storedValueActivityVO = storedValueActivityService.getOngoingActivity();
        return Response.success(storedValueActivityVO);
    }

    @Operation(summary = "获取余额变动明细列表")
    @GetMapping("/bill/list")
    public Response<List<ClientBillVO>> getClientBillList(ClientBillReqDTO clientBill) {
        List<ClientBillVO> clientBillVOList = clientBillService.getClientBillList(clientBill);
        return Response.success(clientBillVOList);
    }

    @Operation(summary = "立即充值-创建订单")
    @PostMapping("/recharge/settle")
    public R<String> rechargeSettle(@RequestBody BalanceRechargeSettleDTO settleDTO) {
        String orderSn = clientBillService.rechargeSettle(settleDTO.getActivityId(), settleDTO.getActivityDetailId());
        return R.ok(orderSn);
    }

    @GetMapping("/recharge/settle/payment")
    @Operation(summary = "立即充值-调用微信接口")
    public R<WxPayUnifiedOrderV3Result.JsapiResult> rechargeSettlePayment(@Schema(description = "订单编号") @Parameter String orderSn) {
        WxPayUnifiedOrderV3Result.JsapiResult jsapiResult = clientBillService.rechargeSettlePayment(orderSn);
        return R.ok(jsapiResult);
    }

    @Operation(summary = "判断用户是否存在支付密码")
    @GetMapping("/hasPayPassword")
    public Response<Long> hasPayPassword() {
        Boolean hasPayPassword = clientService.hasPayPassword();
        Long result = BooleanUtil.toLongObj(hasPayPassword);
        return Response.success(result);
    }

    @Operation(summary = "设置支付密码")
    @PostMapping("/setPayPassword")
    public Response<Void> setPayPassword(@RequestBody PayPasswordDTO payPasswordDTO) throws Exception {
        // payPasswordDTO.setFirstPassword(RsaUtils.decryptByPrivateKey(payPasswordDTO.getFirstPassword()));
        // payPasswordDTO.setSecondPassword(RsaUtils.decryptByPrivateKey(payPasswordDTO.getSecondPassword()));
        clientService.setPayPassword(payPasswordDTO);
        return Response.success();
    }

    @Operation(summary = "重置支付密码-获取验证码")
    @GetMapping("/resetPayPassword/code")
    public Response<Void> resetPayPasswordGetCode() {
        clientService.resetPayPasswordGetCode();
        return Response.success();
    }

    @Operation(summary = "重置支付密码-校验密码‘下一步’按钮")
    @PostMapping("/resetPayPassword/code/verify")
    public Response<Boolean> resetPayPasswordVerifyCode(@RequestBody ResetPasswordCode resetPasswordCode) {
        Boolean b = clientService.resetPayPasswordVerifyCode(resetPasswordCode.getCode());
        return Response.success(b);
    }

    @Operation(summary = "免密支付按钮状态")
    @GetMapping("/payPasswordFree")
    public Response<Integer> payPasswordFree() {
        Integer payPasswordFree = clientService.payPasswordFree();
        return Response.success(payPasswordFree);
    }

    @Operation(summary = "免密支付按钮状态修改")
    @PostMapping("/payPasswordFree")
    public Response<Void> payPasswordFree(@RequestBody PayPasswordFreeDTO payPasswordFreeDTO) throws Exception {
        // payPasswordFreeDTO.setPassword(RsaUtils.decryptByPrivateKey(payPasswordFreeDTO.getPassword()));
        clientService.payPasswordFree(payPasswordFreeDTO);
        return Response.success();
    }


}
